// 触摸控制系统
class TouchController {
    constructor(scene) {
        this.scene = scene;
        this.touchZone = null;
        this.virtualJoystick = null;
        this.touchButtons = {};
        this.isMobile = GameConfig.isMobile();
        
        // 触摸移动数据
        this.touchInput = {
            isActive: false,
            startX: 0,
            startY: 0,
            currentX: 0,
            currentY: 0,
            deltaX: 0,
            deltaY: 0
        };
        
        if (this.isMobile) {
            this.createTouchControls();
        }
    }
    
    createTouchControls() {
        // 创建全屏触摸区域
        this.createFullScreenTouch();
        
        // 创建触摸按钮
        this.createTouchButtons();
    }
    
    createFullScreenTouch() {
        const gameWidth = this.scene.game.config.width;
        const gameHeight = this.scene.game.config.height;
        
        // 创建覆盖整个屏幕的透明触摸区域
        this.touchZone = this.scene.add.rectangle(gameWidth/2, gameHeight/2, gameWidth, gameHeight, 0x000000, 0)
            .setScrollFactor(0)
            .setDepth(500)
            .setInteractive();
        
        // 设置触摸事件
        this.touchZone.on('pointerdown', (pointer) => {
            this.touchInput.isActive = true;
            this.touchInput.startX = pointer.x;
            this.touchInput.startY = pointer.y;
            this.touchInput.currentX = pointer.x;
            this.touchInput.currentY = pointer.y;
            this.touchInput.deltaX = 0;
            this.touchInput.deltaY = 0;
        });
        
        this.touchZone.on('pointermove', (pointer) => {
            if (this.touchInput.isActive) {
                this.touchInput.currentX = pointer.x;
                this.touchInput.currentY = pointer.y;
                
                // 计算相对于起始点的偏移
                this.touchInput.deltaX = this.touchInput.currentX - this.touchInput.startX;
                this.touchInput.deltaY = this.touchInput.currentY - this.touchInput.startY;
                
                // 限制最大移动距离
                const maxDistance = 100;
                const distance = Math.sqrt(this.touchInput.deltaX * this.touchInput.deltaX + this.touchInput.deltaY * this.touchInput.deltaY);
                
                if (distance > maxDistance) {
                    const ratio = maxDistance / distance;
                    this.touchInput.deltaX *= ratio;
                    this.touchInput.deltaY *= ratio;
                }
            }
        });
        
        this.touchZone.on('pointerup', () => {
            this.touchInput.isActive = false;
            this.touchInput.deltaX = 0;
            this.touchInput.deltaY = 0;
        });
        
        // 显示触摸指示器（可选）
        this.createTouchIndicator();
    }
    
    createTouchIndicator() {
        // 创建触摸点指示器
        this.touchIndicator = this.scene.add.circle(0, 0, 30, 0xffffff, 0.3)
            .setScrollFactor(0)
            .setDepth(600)
            .setVisible(false);
            
        // 创建移动方向指示器
        this.directionIndicator = this.scene.add.circle(0, 0, 15, 0x00ff00, 0.6)
            .setScrollFactor(0)
            .setDepth(601)
            .setVisible(false);
    }
    
    createTouchButtons() {
        const buttonSize = 60;
        const margin = 20;
        const gameWidth = this.scene.game.config.width;
        const gameHeight = this.scene.game.config.height;
        
        // 暂停按钮
        this.touchButtons.pause = this.scene.add.circle(
            gameWidth - margin - buttonSize/2,
            margin + buttonSize/2,
            buttonSize/2,
            0x3498db,
            0.7
        ).setScrollFactor(0).setDepth(1000);
        
        this.scene.add.text(
            gameWidth - margin - buttonSize/2,
            margin + buttonSize/2,
            '⏸️',
            { fontSize: '24px' }
        ).setOrigin(0.5).setScrollFactor(0).setDepth(1001);
        
        // 升级按钮
        this.touchButtons.upgrade = this.scene.add.circle(
            gameWidth - margin - buttonSize/2,
            margin * 2 + buttonSize * 1.5,
            buttonSize/2,
            0xe74c3c,
            0.7
        ).setScrollFactor(0).setDepth(1000);
        
        this.scene.add.text(
            gameWidth - margin - buttonSize/2,
            margin * 2 + buttonSize * 1.5,
            '🔧',
            { fontSize: '24px' }
        ).setOrigin(0.5).setScrollFactor(0).setDepth(1001);
        
        // 设置按钮交互
        this.setupButtonEvents();
    }
    
    setupButtonEvents() {
        // 暂停按钮事件
        this.touchButtons.pause.setInteractive();
        this.touchButtons.pause.on('pointerdown', () => {
            // 调用游戏的togglePause方法
            if (this.scene.togglePause) {
                this.scene.togglePause();
            }
        });
        
        // 升级按钮事件
        this.touchButtons.upgrade.setInteractive();
        this.touchButtons.upgrade.on('pointerdown', () => {
            if (this.scene.uiSystem) {
                this.scene.uiSystem.showUpgradeMenu();
            }
        });
        
        // 添加触摸视觉反馈
        Object.values(this.touchButtons).forEach(button => {
            button.on('pointerover', () => {
                button.setScale(1.1);
            });
            
            button.on('pointerout', () => {
                button.setScale(1.0);
            });
            
            button.on('pointerdown', () => {
                button.setScale(0.9);
            });
            
            button.on('pointerup', () => {
                button.setScale(1.0);
            });
        });
    }
    
    getMovementInput() {
        if (!this.isMobile || !this.touchInput.isActive) {
            return { x: 0, y: 0 };
        }
        
        // 更新视觉指示器
        if (this.touchIndicator && this.directionIndicator) {
            this.touchIndicator.setPosition(this.touchInput.startX, this.touchInput.startY);
            this.touchIndicator.setVisible(true);
            
            this.directionIndicator.setPosition(
                this.touchInput.startX + this.touchInput.deltaX,
                this.touchInput.startY + this.touchInput.deltaY
            );
            this.directionIndicator.setVisible(true);
        }
        
        // 将像素偏移转换为标准化的移动向量
        const normalizedX = this.touchInput.deltaX / 100;
        const normalizedY = this.touchInput.deltaY / 100;
        
        return {
            x: Math.max(-1, Math.min(1, normalizedX)),
            y: Math.max(-1, Math.min(1, normalizedY))
        };
    }
    
    update() {
        // 如果没有触摸活动，隐藏指示器
        if (!this.touchInput.isActive && this.touchIndicator) {
            this.touchIndicator.setVisible(false);
            this.directionIndicator.setVisible(false);
        }
    }
    
    destroy() {
        if (this.touchZone) this.touchZone.destroy();
        if (this.touchIndicator) this.touchIndicator.destroy();
        if (this.directionIndicator) this.directionIndicator.destroy();
        
        Object.values(this.touchButtons).forEach(button => {
            if (button) button.destroy();
        });
    }
}